package com.example.yiqiuser.service.impl;


import com.example.yiqiuser.dao.RoleDao;
import com.example.yiqiuser.entity.Menu;
import com.example.yiqiuser.entity.Role;
import com.example.yiqiuser.entity.User;
import com.example.yiqiuser.service.RoleService;
import com.example.yiqiuser.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ fileName:RoleServiceImpl
 * @ description:
 * @ author:xjd
 * @ createTime:2023/3/28 13:15
 * @ version:1.0.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao;

    /*@Override
    public List<String> queryAllRole() {
        return roleDao.queryAllRole();
    }*/

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public PageInfo queryByParam(Page<Role> page) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        return new PageInfo(roleDao.queryByParam(page.getData()));
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Role> queryAll() {
        return roleDao.queryAll();
    }

    /**
     * 根据id查询
     * @param roleId
     * @return
     */
    @Override
    public Role queryById(Integer roleId) {
        List<Role> roleList = roleDao.queryById(roleId);
        if (roleList!=null&&roleList.size()>0){
            return roleList.get(0);
        }
        return null;
    }
    /**
     * 添加
     * @param role
     * @return
     */
    @Override
    public int add(Role role) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dateStr = sdf.format(date);
        role.setCreateTime(dateStr);
        return roleDao.add(role);
    }

    /**
     * 更新
     * @param role
     * @return
     */
    @Override
    public int update(Role role) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dateStr = sdf.format(date);
        role.setUpdateTime(dateStr);
        return roleDao.update(role);
    }

    /**
     * 根据id删除
     * @param roleId
     * @return
     */
    @Override
    public int deleteById(Integer roleId) {
        return roleDao.deleteById(roleId);
    }

    /**
     * 查询用户的角色返回角色ID数组
     *
     * @param user
     * @return
     */
    @Override
    public ArrayList queryRole(User user) {
        ArrayList arrayList = new ArrayList();
        List<Role> roleList = roleDao.queryRole(user);
        if (roleList != null && roleList.size() != 0) {
            for (Role role : roleList) {
                int roleId = role.getRoleId();
                arrayList.add(roleId);
            }
        }
//        System.out.println("================="+user);
//        System.out.println("================================================"+arrayList);
        return arrayList;
    }

    /**
     * 更新角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @Override
    public int updateRole(Integer userId, ArrayList roleIds) {
        roleDao.deleteRole(userId);
        User user = new User();
        for (Object roleId : roleIds) {
            System.out.println(userId);
            System.out.println(roleId);
            user.setUserId(userId);
            user.setRoleId(Integer.valueOf(roleId + ""));
            roleDao.addRole(user);
        }
        return 1;
    }

    @Override
    public ArrayList queryMenuId(Integer roleId) {
        ArrayList arrayList = new ArrayList();
        List<Menu> menuList = roleDao.queryMenuId(roleId);
        for (Menu menu : menuList) {
            arrayList.add(menu.getMenuId());
        }
        return arrayList;
    }

    @Override
    public int addPosAndMenu(Integer roleId, String menuIds) {
        Menu menu=new Menu();
        menu.setRoleId(roleId);
        roleDao.deleteMenu(roleId);
        ArrayList<Integer> arrayList=new ArrayList();
        String[] str=menuIds.split(",");
        for (String s : str) {
            arrayList.add(Integer.valueOf(s+""));
        }
        System.out.println("==================="+arrayList);
        for (Integer o : arrayList) {
            menu.setMenuId(o);
            roleDao.addMenu(menu);
        }
        return 1;
    }

    /**
     * 查询角色权限关联表  根据角色ID查询该角色关联的所有权限ID集合
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> queryMenuIdListByRoleId(Integer roleId) {
        List<Integer> integerList = roleDao.queryMenuIdListByRoleId(roleId);
        if (integerList!=null&&integerList.size()>0){
            return integerList;
        }
        return null;
    }

    /**
     * 根据角色ID批量删除该角色对应角色权限关联
     * @param roleId
     * @return
     */
    @Override
    public int deleteMenuByRoleId(Integer roleId) {
        return roleDao.deleteMenuByRoleId(roleId);
    }

    /**
     * 添加职位（角色）和权限的关联
     * @param roleId
     * @param menuId
     * @return
     */
    @Transactional //保证该方法中的调用的所有方法一起提交或者一起回滚
    @Override
    public int addRoleAndMenu(int roleId, String menuId) {
        //1.先调用角色的根据id删除角色权限关联表的方法
        this.deleteMenuByRoleId(roleId);

        //2.再添加关联
        //添加是否成功标识符
        boolean flag = true;

        //判断menuId是否为空 不为空时进行 分割  添加 操作
        if (!StringUtils.isEmpty(menuId)){
            //进行分割为数组
            String[] ids = menuId.split(",");
            //循环数组
            for (String id : ids) {
                //调用addRoleAndMenu方法进行添加所选的menuId进行添加
                int sucNum = roleDao.addRoleAndMenu(roleId, Integer.valueOf(id));
                if (sucNum<1){
                    //说明调用失败
                    flag=false;
                }
            }
        }
        //如果循环添加结束，flag仍然是true说明 全部成功
        if(flag){
            return 0;
        }
        return 1;
    }
}
